import cv2
import numpy as np
import math
from math import *
import PickBlue

from skimage.measure import label

if __name__ == "__main__":
    img = cv2.imread("receipt_img/ticket2.jpg")

    img_origin = img.copy()
    h, w, _ = img.shape
    cv2.imshow('origin', img)

    print("将蓝色部分提取出来")
    border_img = cv2.Canny(img, 50, 150)
    bh, bw = border_img.shape[:2]
    for j in range(bh):
        for k in range(bw):
            border_img[j][k] = 255 - border_img[j][k]
    border_img = PickBlue.dilate_demo(border_img, 2)

    for i in range(0, h):
        for j in range(0, w):
            B = float(img[i][j][0])
            G = float(img[i][j][1])
            R = float(img[i][j][2])
            if R + G + B < 400:
                area_sum = 0
                canny_sum = 0
                for x in range(i-2, i+2, 1):
                    for y in range(j-2, j+2):
                        if x < h and y < w:
                            area_sum += 1
                            if border_img[x, y] == 0:
                                canny_sum += 1
                if canny_sum/area_sum < 0.7:
                    img[i][j] = [255, 255, 255]
            else:
                img[i][j] = [255, 255, 255]

    cv2.imshow('pickBlue', img)
    cv2.imshow("borderImage", border_img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()